<?php
class ArticlesController extends AppController {

	var $name = 'Articles';
	var $uses = array('Article','Rating');
	var $helpers = array('Rss','Ajax','ArticleMenu','Time');
	var $components = array('Seo');
	var $created = null;
	var $backupKey = "AsedFDzxc_backup_article";

	function beforeFilter()	{
		parent::beforeFilter();
		@$this->created = '%'.$this -> params['year'] . '-' . $this -> params['month'] . '-' . $this -> params['day'] . '%';
	}
	
	function admin_index() {
		$this->Article->recursive = 0;
		$this -> paginate = array('order' => 'Article.created desc');
		$this->set('articles', $this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->data['Article']['seo_link'] = $this->Seo->create($this->data['Article']['title']);

			//ak sme vykonali zalohu clanku, len ju aktualizujeme
			if($this->Session->read($this->backupKey)) {
				$this->data['Article']['id'] = $this->Session->read($this->backupKey);
			} else {
				$this->Article->create();
			}
			
			if ($this->Article->save($this->data)) {
				$this->Session->setFlash(__('ARTICLE_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash(__('ARTICLE_SAVED_ERROR',true));
			}
		} else {
			//vymazeme zalohu clanku
			$this->Session->delete($this->backupKey);
		}
		$categories = $this->Article->Category->find('list');
		$users = $this->Article->User->find('list');
		$tags = $this->Article->Tag->find('list');
		$this->set(compact('users','categories','tags'));
	}

	function admin_edit($id = null) {
		if (!$this -> Article -> findById($id)) {
			$this->Session->setFlash(__('ARTICLE_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		if (!empty($this->data)) {
			$this->data['Article']['seo_link'] = $this->Seo->create($this -> data['Article']['title']);
			if ($this->Article->save($this->data)) {
				$this->Session->setFlash(__('ARTICLE_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash(__('ARTICLE_SAVED_ERROR',true));
			}
		} else {
			$this->data = $this->Article->findById($id);
			$this->Session->write($this->backupKey,$this->data["Article"]["id"]);
		}
		$categories = $this->Article->Category->find('list');
		$users = $this->Article->User->find('list');
		$tags = $this->Article->Tag->find('list');
		$this->set(compact('users','categories','tags'));
	}

	function admin_delete($id = null) {
		if (!$this->Article->findById($id)) {
			$this->Session->setFlash(__('ARTICLE_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		if ($this->Article->del($id)) {
			$this->Session->setFlash(__('ARTICLE_DELETED_OK',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

	function admin_delete_articles() {
		if($this->Article->deleteAll(array('Article.id'=>$this->data['ArticlesDelete']))) {
			$this->Session->setFlash(__('ARTICLES_DELETED_OK',true));
		} else {
			$this->Session->setFlash(__('ARTICLES_DELETED_ERROR',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}

	function admin_preview() {
		$preview = $this->data['Article'];
		$this->set(compact("preview"));
	}

	function admin_backup() {
		$this->data["Article"]["visible"] = 0;

		if($this->Session->check($this->backupKey)) {
			$this->data["Article"]["id"] = $this->Session->read($this->backupKey);
		}
		if($this->Article->save($this->data)) {
			$this->Session->setFlash(__('ARTICLE_SAVED_OK',true));
		} else {
			$this->Session->setFlash(__('ARTICLE_SAVED_ERROR',true));
		}
		$this->Session->write($this->backupKey,$this->Article->getLastInsertID());
	}

	function rss() {
		$this->set('articles',$this->Article->findAll(array('Article.visible=1','Article.publish_up < now()'),null,array('Article.created'=>'desc')));
	}

	function index() {
		$this->Article->recursive = 1;
		$this->paginate = array('limit'=>Configure::read('Homepage.articles'),'order' => 'Article.created desc','conditions'=>array('show_on_index'=>1,'visible'=>1,'publish_up<=now()'));
		$this->set('articles', $this->paginate());
	}

	function view($articleSeoLink = null) {
		$this->Article->updateAll(array('visited'=>'visited+1'),array('Article.seo_link' => $articleSeoLink,'Article.created like "'.$this->created.'"'));
		$article = $this->Article->find(array('Article.seo_link' => $articleSeoLink,'Article.created like "'.$this->created.'"'));
		$url = $this->params['year'].'/'.$this->params['month'].'/'.$this->params['day'].'/'.$articleSeoLink;
		$recentArticles = $this->Article->findAll(array('Article.category_id'=>$article['Article']['category_id']),array('Article.title','Article.created','Article.seo_link'),'Article.created desc',5,1,0);
		$this->set(compact("article","url","recentArticles"));
	}

	function comment_add($articleSeoLink = null) {
		if(!$this->Article->find(array('Article.seo_link' => $articleSeoLink,'Article.created like "'.$this->created.'"'))) {
			$this->Session->setFlash(__('COMMENT_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		} else if (!empty($this->data)) {
			$this->Comment->create();
            $this->data['Comment']['antispam'] = $this->data['Comment']['badinput'];
			if ($this->Article->Comment->save($this->data)) {
				if(!$this->RequestHandler->isAjax()) {
					$this->Session->setFlash(__('COMMENT_SAVED_OK',true));
					$url = $this->params['year'].'/'.$this->params['month'].'/'.$this->params['day'].'/'.$articleSeoLink.'/#comment_'.$this->Article->Comment->getInsertId();
					$this->redirect(array('action'=>$url),null,true);
				} else {
					$this->set("comment",$this->Article->Comment->findById($this->Comment->getInsertId()));
				}
			} else {
				$this->Session->setFlash(__('COMMENT_SAVED_ERROR',true));
			}
		}

		if($this->RequestHandler->isAjax()) {
			$this->action = "comment_action";
		}
	}

	function comment_preview() {
		$this->set('preview',$this->data['Comment']);
	}

	function rating($articleSeoLink,$ratingValue) {
		$id = $this->Article->find(array('Article.seo_link' => $articleSeoLink,'Article.created like "'.$this->created.'"'),"id",null,0);
		$id = $id['Article']['id'];

		$data = array('article_id'=>$id,'value'=>$ratingValue,'ip_address'=>$this->RequestHandler->getClientIp());

		if(!$this->Rating->findByIp($this->RequestHandler->getClientIp(),$id)) {
			if($this->Rating->save($data)) {
				$this->Session->setFlash(__('RATING_SAVED_OK',true));
			}
		} else {
			$this->Session->setFlash(__('RATING_MORE_TIMES',true));
		}

		if($this->RequestHandler->isAjax()) {
			$this->action = "rating_action";
		} else {
			$url = $this->params['year'].'/'.$this->params['month'].'/'.$this->params['day'].'/'.$articleSeoLink;
			$this->redirect(array('action'=>$url),null,true);
		}
	}
}
?>